package com.guigutool.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guigutool.reggie.common.R;
import com.guigutool.reggie.dto.SetmealDto;
import com.guigutool.reggie.entity.Category;
import com.guigutool.reggie.entity.Setmeal;
import com.guigutool.reggie.service.CategoryService;
import com.guigutool.reggie.service.SetmealDishService;
import com.guigutool.reggie.service.SetmealService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @Author: KingWang
 * @Date: 2023/3/14
 * @Desc:
 **/
@RequestMapping("/setmeal")
@Slf4j
@RestController
@Api(tags="套餐相关接口")
public class SetmealController {

    private final String cacheName = "setmealCache";

    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private RedisTemplate redisTemplate;


    /**
     * 新增套餐
     * @param setmealDto
     * @return
     */
    @ApiOperation(value = "新增套餐")
    @PostMapping
    @CacheEvict(value = cacheName, allEntries = true)
    public R<String> save(@RequestBody SetmealDto setmealDto){

        setmealService.saveWithDish(setmealDto);
        Set keys = redisTemplate.keys("setmeal:*");
        redisTemplate.delete(keys);

        return R.success("新增套餐成功");
    }

    @ApiOperation(value = "套餐分页查询")
    @GetMapping("/page")
    public R<Page> page(String name, int page,int pageSize){
        Page<Setmeal> pageInfo = new Page(page,pageSize);

        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(null != name, Setmeal::getName,name);
        setmealService.page(pageInfo, queryWrapper);

        Page<SetmealDto> setmealDtoPage = new Page<>(page,pageSize);
        BeanUtils.copyProperties(pageInfo,setmealDtoPage,"records");

        List<SetmealDto> setmealDtoList =  pageInfo.getRecords().stream().map(x-> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(x,setmealDto);
            Category category = categoryService.getById(x.getCategoryId());
            setmealDto.setCategoryName(category.getName());
            return setmealDto;
        }).collect(Collectors.toList());
        setmealDtoPage.setRecords(setmealDtoList);

        return R.success(setmealDtoPage);
    }

    /**
     * 删除套餐
     * @param ids
     * @return
     *
     * allEntries=true 清理指定key下的所有数据
     */
    @ApiOperation(value = "删除套餐")
    @DeleteMapping
    @CacheEvict(value = cacheName, allEntries = true)
    public R<String> delete(@RequestParam List<String> ids){
        log.info("delete ids: {}",ids);
        setmealService.removeWithDish(ids);
        Set keys = redisTemplate.keys("setmeal:*");
        redisTemplate.delete(keys);
        return R.success("套餐删除成功");
    }

    @ApiOperation(value = "套餐列表查询")
    @Cacheable(value = cacheName,key = "#setmeal.categoryId+':'+ #setmeal.status")
    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal){
        List<Setmeal> setmealList = null;
        String key = "setmeal:" + setmeal.getCategoryId();
        setmealList = (List<Setmeal>)redisTemplate.opsForValue().get(key);
        if(Objects.nonNull(setmealList)){
            return R.success(setmealList);
        }
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Objects.nonNull(setmeal.getId()),Setmeal::getId,setmeal.getId());
        wrapper.eq(Objects.nonNull(setmeal.getCategoryId()),Setmeal::getCategoryId,setmeal.getCategoryId());
        wrapper.eq(Objects.nonNull(setmeal.getStatus()),Setmeal::getStatus,setmeal.getStatus());
        wrapper.orderByDesc(Setmeal::getUpdateTime);
        setmealList = setmealService.list(wrapper);

        redisTemplate.opsForValue().set(key, setmealList);

        return R.success(setmealList);
    }
}
